package net.huashitong.supervision.task.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.SpecialTask;
import net.huashitong.supervision.task.filter.SpecialTaskFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author ling
 * @date: 2020/2/25
 * @time: 10:10
 * @version: 1.0
 * @Description:
 */
@Repository
public class SpecialTaskDao extends StringDao<SpecialTask> {

    /**
     * 条件查询特殊任务列表
     * @param filter
     * @return
     */
    public List<SpecialTask> queryFilter(SpecialTaskFilter filter) {
        GenericQuery obj = createQuery("obj");
       /* if (StringUtils.isNotBlank(filter.getOverdueDeptId())){
            obj.append(" and overdueDeptId =:overdueDeptId").setParam("overdueDeptId",filter.getOverdueDeptId());
        }*/
        if (StringUtils.isNotBlank(filter.getTaskType())){
            obj.append( " and taskType =:taskType").setParam("taskType",filter.getTaskType());
        }
        if (StringUtils.isNotBlank(filter.getTimeRound())){
            obj.append(" and timeRound =:timeRound").setParam("timeRound",filter.getTimeRound());
        }
        if (StringUtils.isNotBlank(filter.getStatus())){
            obj.append(" and status=:status").setParam("status",filter.getStatus());
        }
        if (StringUtils.isNotBlank(filter.getType())){
            if("1".equals(filter.getType()))//1 待反馈，待反馈 包括 待反馈 和 退回
            {
                obj.append(" and ( type ='1' or type='3' )" );

            }else if("2".equals(filter.getType()))
            {
                obj.append(" and ( type ='2' or type='4' )" );
            }


            else {

                obj.append(" and type =:type").setParam("type",filter.getType());
            }

        }
         if (StringUtils.isNotBlank(filter.getTaskMainName())){
             obj.append(" and taskMainName like:taskMainName").setParam("taskMainName",StringUtils.getLikewords(filter.getTaskMainName()));
         }
         if (StringUtils.isNotBlank(filter.getTaskMainId())){
             obj.append(" and taskMainId =:taskMainId").setParam("taskMainId",filter.getTaskMainId());
         }
         if (StringUtils.isNotBlank(filter.getCreateUser())||StringUtils.isNotBlank(filter.getOverdueDeptId())){
             if(StringUtils.isNotBlank(filter.getCreateUser())&&StringUtils.isNotBlank(filter.getOverdueDeptId()))
             {
                 obj.append(" and  ( overdueDeptId =:overdueDeptId").setParam("overdueDeptId",filter.getOverdueDeptId());
                 obj.append(" or  createUser =:createUser  )").setParam("createUser",filter.getCreateUser());


             }else if(StringUtils.isBlank(filter.getCreateUser())&&StringUtils.isNotBlank(filter.getOverdueDeptId())){
                 obj.append(" and overdueDeptId =:overdueDeptId").setParam("overdueDeptId",filter.getOverdueDeptId());

             }else {

                 obj.append(" and createUser =:createUser").setParam("createUser",filter.getCreateUser());
             }

         }
         if(StringUtils.isNotBlank(filter.getInspectionId()))
         {
             obj.append(" and inspectionId=:inspectionId").setParam("inspectionId",filter.getInspectionId());
         }
         obj.setOrder("sysId+0",GenericQuery.DESC);
         return obj.listResult(filter);
    }
}
